package cn.bluemobi.dylan.step.moder.prefs;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by $Liudan on 2018/8/9 0009.
 */
public class SharedPreferencesHelper {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    public SharedPreferencesHelper(Context context, String FILE_NAME) {
        sharedPreferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }


    public String getPrefString(String key, String defaultValue) {
        return sharedPreferences.getString(key, defaultValue);
    }
    public void setPrefString(String key, String value) {
        editor.putString(key, value).commit();
    }
    public boolean getPrefBoolean(String key, boolean defaultValue) {
        return sharedPreferences.getBoolean(key, defaultValue);
    }
    public void setPrefBoolean(String key, boolean defaultValue) {
        editor.putBoolean(key, defaultValue).commit();
    }
    public float getPrefFloat(String key, float defaultValue) {
        return sharedPreferences.getFloat(key, defaultValue);
    }
    public void setPrefFloat(String key, float defaultValue) {
        editor.putFloat(key, defaultValue).commit();
    }
    public long getPrefLong(String key, long defaultValue) {
        return sharedPreferences.getLong(key, defaultValue);
    }
    public void setPrefLong(String key, long defaultValue) {
        editor.putLong(key, defaultValue).commit();
    }
    public int getPrefInt(String key, int defaultValue) {
        return sharedPreferences.getInt(key, defaultValue);
    }
    public void setPrefInt(String key, int defaultValue) {
        editor.putInt(key, defaultValue).commit();
    }

    /**
     * 移除某个key值已经对应的值
     */
    public void remove(String key) {
        editor.remove(key);
        editor.commit();
    }

    /**
     * 清除所有数据
     */
    public void clear() {
        editor.clear();
        editor.commit();
    }

    /**
     * 查询某个key是否存在
     */
    public Boolean contain(String key) {
        return sharedPreferences.contains(key);
    }

    /**
     * 返回所有的键值对
     */
    public Map<String, ?> getAll() {
        return sharedPreferences.getAll();
    }

    /**
     * 存储对象
     *
     * @param key
     * @param t
     * @param <T>
     * @return
     */
    public <T> T setPrefObject(String key, T t) {
        String toJsonStr = new Gson().toJson(t);
        editor.putString(key, toJsonStr).commit();
        return t;
    }

    /**
     * 获取对象
     *
     * @param key
     * @param classOfT
     * @param <T>
     * @return
     */
    public <T> T getPrefObject(String key, Class<T> classOfT) {
        String jsonStr = sharedPreferences.getString(key, "");
        T t = null;
        if (jsonStr.length() > 0) {
            t = new Gson().fromJson(jsonStr, classOfT);
        }
        return t;
    }


    /**
     * 存储list
     *
     * @param key
     * @param <T>
     * @return
     */
    public <T> void setPrefObjectArray(String key, List<T> datalist) {
        if (null == datalist || datalist.size() <= 0) return;
        String toJsonStr = new Gson().toJson(datalist);
        editor.putString(key, toJsonStr).commit();
    }

    /**
     * 获取list
     *
     * @param key
     * @param <T>
     * @return
     */
    public <T> List<T> getPrefObjectArray(String key,T t) {
        List<T> datalist = new ArrayList<T>();
        String jsonStr = sharedPreferences.getString(key, "");
        if (jsonStr.length() > 0) {
            Gson gson = new Gson();
            datalist = gson.fromJson(jsonStr, new TypeToken<List<T>>() {}.getType());
        }
        return datalist;
    }
}
